<?php if (empty($this->typeUri)) { ?>
    <h2>Browse by type</h2>
    <div id="chart" style="float:right;"></div>
    
    <div>
        <p id="status">Loading...</p>
        <ul id="typesList">
            <?php foreach ($this->types as $i=>$typeUri) { ?>
                <?php $query = 'select ?subject where {?subject a <'.$typeUri.'>}'; ?>
                <li class="type"><a href="/store/types?type=<?php echo urlencode($typeUri); ?>"><?php echo $typeUri; ?></a></li>
            <?php } ?>
        </ul>
    </div>
    
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load('visualization', '1', {'packages':['piechart']});
    </script>
    <script type="text/javascript" src="/js/chart.js"></script>
    <script type="text/javascript">
        $(window).load(function () {
            var types = $('.type');
            var totalTypes = types.length;
            $(types).each(function (i) {
                var typeUri = $(this).text();
                var li = $(this);
                $.ajax({
                    url: '/store/typecount',
                    data: {
                      'type':typeUri
                    },
                    success: function(typeCount) {
                        $('#status').html('Counting: '+(i+1)+'/'+totalTypes);
                        $(li).append('&nbsp;('+typeCount+')');
                        addChartData(typeUri, typeCount);
                        if (i+1 == totalTypes)
                        {
                            $('#status').remove();
                        	   drawPieChart();
                        }
                    }
                });
            });
        });
    </script>
    
<?php } else { ?>
    <h2>Browse by type: <?php echo htmlspecialchars($this->typeUri); ?></h2>

    <?php if ($this->resourceList->total_results == 0) { ?>
        <p>No data found</p>
    <?php } else { ?>
        <p>Results = <?php echo $this->resourceList->total_results; ?></p>
        <ul>
            <?php foreach ($this->resourceList->items as $i=>$resourceListEntry) { ?>
                <?php $uri = $resourceListEntry['http://purl.org/rss/1.0/link'][0]; ?>
                <li><a href="/store/browse?uri=<?php echo urlencode($uri); ?>"><?php echo htmlspecialchars($uri); ?></a></li>
            <?php } ?>
        </ul>

        <p>
        <?php 
            $startIndex = $this->resourceList->start_index;
            $itemsPerPage = $this->resourceList->items_per_page;
            $totalResults = $this->resourceList->total_results;
            
            if ($startIndex - $itemsPerPage >= 0)
            {
                $encodedTypeUri = urlencode($this->typeUri);
                $newStartIndex = $startIndex - $itemsPerPage;
                if ($newStartIndex < 0)
                {
                    $newStartIndex = 0;
                }
                echo "<a href=\"/store/types?type=$encodedTypeUri&offset=$newStartIndex\">Prev</a>&nbsp;";
            }
            if ($startIndex + $itemsPerPage < $totalResults)
            {
                $encodedTypeUri = urlencode($this->typeUri);
                $newStartIndex = $startIndex + $itemsPerPage;
                echo "<a href=\"/store/types?type=$encodedTypeUri&offset=$newStartIndex\">Next</a>";
            }
            ?>
        </p>
        <?php //var_dump($this->resourceList); ?>
    <?php } ?>
<?php } ?>